(function() {
	var ics = Iefgames_CS;
	if ($defined(ics.Comet)) {
		return ;
	}
	
	ics.Comet = function() {
		var port = 8080;
		var uri = ics.Core.baseUri + (port == 80 ? '' : ':' + port) + '/comet';

		//comet标志，用于判断是否已经在comet
		var isComet = false;
		
		var iframeId = 'cometIframe';
		
		//把sessionId存入cookie，以便读取
		var href = window.location.href;
		var index = href.indexOf('?');
		if (index == -1) {
			//异常，退出
			return ;
		}
		
		Cookie.write(ics.Cookie.sessionCookieName, href.substring(index+1, href.length), ics.Cookie.Options);
		
		var createIframe = function() {
			var iframe = $(iframeId);
			if (!iframe 
				|| $type(iframe) != 'element') {
				iframe = new IFrame({
					'id': iframeId,
					'src': 'http://im.cs.iefgames.com/iframe.html',
					'style': {
						'visibility': 'hidden'
					}
				});
				
				$(document.body).grab(iframe);
			}
			
			return iframe;
		}
		
		var disposeIframe = function() {
			$(iframeId).dispose();
		}
		
		var start = function() {
			//cs第一次连接comet时并没有uuid，需要从数据库得到
			var uuid = null;
			if (isComet) {
				uuid = Cookie.read(ics.Cookie.uuidCookieName);
			}
			//cs的sessionId是成功登录之后通过url得来
			var sessionId = Cookie.read(ics.Cookie.sessionCookieName);
			if (!sessionId || sessionId.length != 32) {
				//sessionId异常，退出
				return ;
			}
			
			var frame = createIframe().setProperty('src', uri + '?' + (!!uuid ? 'uuid=' + uuid : '') + (!!sessionId ? (!!uuid ? '&' : '') + 'sessionId=' + sessionId : '') + (Browser.Engine.trident ? '&browse=ie' : '') + '&timestamp=' + new Date().getTime());
		}
		
		var stop = function() {
			isComet = false;
			disposeIframe();
		}
		
		var restart = function() {
			start();
		}
		
		var realStart = function(csUuid) {
			if (!csUuid || csUuid.length != 32) {
				return;
			}
			
			isComet = true;
			Cookie.write(ics.Cookie.uuidCookieName, csUuid, ics.Cookie.Options);
			
			restart.delay(2000);
		}
		
		
		
		ics._$('comet', realStart);
		start.delay(2000);
		return {
			'stop': stop,
			'start': start
		}
	}()	
})();